﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CuaHangMayTinh.BusinessEntities;
using CuaHangMayTinh.BusinessLogicLayer;
using CuaHangMayTinh.Utilities;
using Microsoft.VisualBasic;

namespace CuaHangMayTinh.PresentationLayer
{
    /// <summary>
    /// form phân quyền
    /// </summary>
    public partial class FormPhanQuyen : Form
    {
        /// <summary>
        /// form khởi tạo
        /// </summary>
        public FormPhanQuyen()
        {
            InitializeComponent();
        }

        /// <summary>
        /// bắt sự kiện laod form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormPhanQuyen_Load(object sender, EventArgs e)
        {
            DataTable dt = SubGroupUserManager.Find_All();
            LoadListView(dataGridView_phanquyen,dt,"Name");
        }

        /// <summary>
        /// sự kiện khi thay đổi row datagrid  phân quyền
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView_phanquyen_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView_phanquyen.SelectedRows.Count > 0)
            {
                var row = dataGridView_phanquyen.SelectedRows[0];
                DataTable dt = QuyenManager.Find_IDSubGroupUser((int)row.Cells[1].Value);
                LoadListView(dataGridView_SubPhanQuyen, dt, "Ten");
            }
            else
            {
                dataGridView_SubPhanQuyen.DataSource = null;
            }
        }

        /// <summary>
        /// sự kiện khi thay đổi datagird sub phân quyền
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SubPhanQuyen_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
//            if (SubPhanQuyen.SelectedRows.Count > 0)
//            {
//                var rowpq = dataGridView_phanquyen.SelectedRows[0];
//                var rowq = SubPhanQuyen.SelectedRows[0];
//                DataTable dt2 = PhannQuyenManager.Find_SubGroupUser_Quyen((int)rowpq.Cells[1].Value,
//                    (int)rowq.Cells[1].Value);
//                PhanQuyenEntity phanQuyenEntity = PhannQuyenManager.ConvertToList(dt2)[0];
//                var c = DataUtil.ConvertBool(phanQuyenEntity.Diem);
//                DataTable dt = new DataTable();
//                dt.Columns.Add("Xem", typeof(bool));
//                dt.Columns.Add("Thêm", typeof(bool));
//                dt.Columns.Add("Xóa", typeof(bool));
//                dt.Columns.Add("Sửa", typeof(bool));
//                dt.Rows.Add(c[0], c[1], c[2], c[3]);
//                dataGridView_thuoctinh.DataSource = dt;
//            }
        }

        /// <summary>
        /// bắt sự kieenh click nút lưu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripBtn_luu_Click(object sender, EventArgs e)
        {
            if (dataGridView_thuoctinh.Rows.Count > 0)
            {
                var rowpq = dataGridView_phanquyen.SelectedRows[0];
                var rowq = dataGridView_SubPhanQuyen.SelectedRows[0];
                var row = dataGridView_thuoctinh.Rows[0].Cells;
                int x = DataUtil.ConvertToInt(new[] { (bool)row[0].EditedFormattedValue, (bool)row[1].EditedFormattedValue, (bool)row[2].EditedFormattedValue, (bool)row[3].EditedFormattedValue });
                if (
                    PhannQuyenManager.Update_Diem(new PhanQuyenEntity((int) rowq.Cells[1].Value,
                        (int) rowpq.Cells[1].Value, x)))
                {
                    MessageBox.Show("Cập nhật thành công !");
                }
                else
                {
                    MessageBox.Show("Cập nhật thất bại !");
                }
            }
        }

        /// <summary>
        /// laod dât table lên datagrid hiển thị columns tên
        /// </summary>
        /// <param name="dataGrid"></param>
        /// <param name="dt"></param>
        /// <param name="ten"></param>
        private void LoadListView(DataGridView dataGrid, DataTable dt, string ten)
        {
            if (dt != null)
            {
                dt.Columns.Add("STT");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dt.Rows[i]["STT"] = i + 1;
                }
                dataGrid.DataSource = dt;
                dataGrid.Columns["STT"].DisplayIndex = 0;
                for (int i = 0; i < dataGrid.Columns.Count; i++)
                {
                    dataGrid.Columns[i].Visible = false;
                }
                dataGrid.Columns["STT"].Visible = true;
                dataGrid.Columns[ten].Visible = true;
            }
            else
            {
                dataGrid.DataSource = null;
            }
        }

        private void thêmToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //goi ham kiem tra quen nao chua co
            if (dataGridView_phanquyen.SelectedRows.Count > 0)
            {
                DataTable dt = QuyenManager.FindNot_IDSubGroupUser((int)dataGridView_phanquyen.SelectedRows[0].Cells[1].Value);
                LoadListView(dataGridView_thuoctinh, dt, "Ten");
            }
        }

        private void toolStripBtn_themquyen_Click(object sender, EventArgs e)
        {
            if (dataGridView_thuoctinh.SelectedRows.Count > 0)
            {
                foreach (DataGridViewRow selectedRow in dataGridView_thuoctinh.SelectedRows)
                {
                    var rowq = dataGridView_phanquyen.SelectedRows[0];
                    //them moi quyen vao 
                    if (PhannQuyenManager.Insert(new PhanQuyenEntity((int)selectedRow.Cells[0].Value, (int)rowq.Cells[1].Value, 0)) > 0)
                    {
                        MessageBox.Show("Thêm quyền " + (string)selectedRow.Cells[1].Value + " thành công !");
                    }
                    else
                    {
                        MessageBox.Show("Thêm quyền " + (string)selectedRow.Cells[1].Value + " thất bại !");
                    }
                }
            }
            dataGridView_phanquyen_RowEnter(null, null);
            thêmToolStripMenuItem_Click(null, null);
        }

        private void xóaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dataGridView_SubPhanQuyen.SelectedRows.Count > 0)
            {
                var subid = (int)dataGridView_phanquyen.SelectedRows[0].Cells[1].Value;
                var idquyen = (int)dataGridView_SubPhanQuyen.SelectedRows[0].Cells[1].Value;
                if (PhannQuyenManager.Delete(idquyen, subid))
                {
                    dataGridView_phanquyen_RowEnter(null, null);
                    MessageBox.Show("Đã xóa quyền" + (string) dataGridView_SubPhanQuyen.SelectedRows[0].Cells[2].Value +
                                    " thành công!");
                }
                else
                {
                    MessageBox.Show("Xóa quyền" + (string) dataGridView_SubPhanQuyen.SelectedRows[0].Cells[2].Value + " thất bại!");
                }
            }
        }

        private void SubPhanQuyen_Click(object sender, EventArgs e)
        {
            dataGridView_thuoctinh.DataSource = null;
        }

        private void SubPhanQuyen_DoubleClick(object sender, EventArgs e)
        {
            if (dataGridView_SubPhanQuyen.SelectedRows.Count > 0)
            {
                var rowpq = dataGridView_phanquyen.SelectedRows[0];
                var rowq = dataGridView_SubPhanQuyen.SelectedRows[0];
                DataTable dt2 = PhannQuyenManager.Find_SubGroupUser_Quyen((int)rowpq.Cells[1].Value,
                    (int)rowq.Cells[1].Value);
                PhanQuyenEntity phanQuyenEntity = PhannQuyenManager.ConvertToList(dt2)[0];
                var c = DataUtil.ConvertBool(phanQuyenEntity.Diem);
                DataTable dt = new DataTable();
                dt.Columns.Add("Tìm Kiếm", typeof(bool));
                dt.Columns.Add("Thêm", typeof(bool));
                dt.Columns.Add("Xóa", typeof(bool));
                dt.Columns.Add("Sửa", typeof(bool));
                dt.Rows.Add(c[0], c[1], c[2], c[3]);
                dataGridView_thuoctinh.DataSource = dt;
            }
        }

    }
}
